'use strict';

    angular.module('app')
        .controller('clientUpdateController', ['$scope', '$rootScope', '$state', '$timeout', 'toaster','Upload',
                                             function($scope, $rootScope, $state, $timeout, toaster,Upload) {
        	
        	$scope.record = {
        		id : $state.params.id
        	};
        	
        	if($state.includes('**.update')){
                $scope.title="编辑应用";
                $scope.reset = false;
                var id = $state.params.id;
                activate(id);
            }else if($state.includes('**.create')){
            	$scope.title="新增应用";
            	$scope.reset = true;//新增时可以重置
                setTimeout(function(){
                    !$rootScope.$$phase && $scope.$apply();
                },300);
            }
        	
        	validate();
        	$scope.resetFrom = function(){
        		$scope.record = {
        			id : $state.params.id
        		}
        	}
        	$scope.submit1= function(){
        		
                var m = $scope.record;
               
                if(m){
                    $scope.isDisabled = true;
                    $.ajax({
        				type: 'POST',
        	            dataType: 'json',
        				contentType:'application/json;charset=UTF-8;multipart/form-data',
        				url : '/clientm/update',
        				data: angular.toJson($scope.record)
        			}).then(function(){
                        if(result.httpCode ==200){//成功
                            toaster.clear('*');
                            toaster.pop('success', '', "保存成功");
                            $timeout(function(){
                                $state.go("main.sys.clientm.list", $state.params);
                            },2000);
                        }else{
                            toaster.clear('*');
                            toaster.pop('error', '', result.msg);
                            $scope.isDisabled = false;
                        }
                    });
                }
                
            }
        	

        	
        	// 初始化页面
            function activate(id) {
    	        $scope.loading = true;
            	$.ajax({
            		type: 'PUT',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : '/clientm/queryAppInfo',
    				data: angular.toJson({'id': id})
    			}).then(function(result) {
    		        $scope.loading = false;
    				if (result.httpCode == 200) {
    					$scope.record = result.data;
    					$scope.record.id = id;//给id值，因为后台需要根据是否有id值来操作：新增（无id）或者修改（有id）
    				} else {
    					$scope.msg = result.msg;
    				}
    				$scope.$apply();
    			});
            }
        	
          //表单校验
        	function validate(){
                jQuery('form').validate({
                	rules: {
                		app_code: {
                    		required: true
                    	},
                    	mac_type: {
                    		required: true
                    	},
                    	app_name: {
                    		required: true
                    	},
                    	app_en_name: {
                    		required: true
                    	},
                    	app_version_seq: {
                    		required: true
                    	},
                    	app_version: {
                    		required: true
                    	},
                    	is_force_upgrade: {
                    		required: true
                    	},
                    	app_version_desc: {
                    		required: true
                    	}
                    	
                    },
                    messages: {
                    	app_code: {
                    		required: '请输入应用编码'
                    	},
                    	mac_type: {
                    		required: '请选择应用系统类型'
                    	},
                    	app_name: {
                    		required: '请输入应用名称'
                    	},
                    	app_en_name: {
                    		required: '请输入应用英文名称'
                    	},
                    	app_version_seq: {
                    		required: '请输入应用版本号(内码)'
                    	},
                    	app_version: {
                    		required: '请输入应用版本'
                    	},
                    	is_force_upgrade: {
                    		required: '请选择是否强制升级'
                    	},
                    	app_version_desc: {
                    		required: '请输入升级包描述'
                    	}
                    },
                    submitHandler: function() {
                        $scope.submit();
                    }
                });
            }
        	
        	
        	 $scope.uploadImg = '';
             //提交
             $scope.submit = function () {
                 $scope.upload($scope.file);
                 console.log($scope.file);
             };
             $scope.upload = function (file) {
                 $scope.fileInfo = file;
                 Upload.upload({
                     //服务端接收
                     url: '/clientm/update',
                     data: {file: file,'params':angular.toJson($scope.record)}
                 }).success(function (result, status, headers, config) {
                     //上传成功
                     if(result.httpCode ==200){//成功
                         toaster.clear('*');
                         toaster.pop('success', '', "保存成功");
                         $timeout(function(){
                             $state.go("main.sys.clientm.list", $state.params);
                         },2000);
                     }else{
                         toaster.clear('*');
                         toaster.pop('error', '', result.msg);
                         $scope.isDisabled = false;
                     }
                    
                 }).error(function (data, status, headers, config) {
                     //上传失败
                     console.log('error status: ' + status);
                 });
             };

    }]);